package com.pda.common.pages.codereset;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import com.apkfuns.logutils.LogUtils;
import com.pda.common.basemvp.model.BaseModel;
import com.pda.common.bean.BaseBean;
import com.pda.common.bean.CodeDeatailBean;
import com.pda.common.beanin.CodeQueryInBean;
import com.pda.common.tools.StringProcess;
import com.pda.common.utils.PDAUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class CodeResetModelImpl extends BaseModel implements CodeResetModel {
    private String tag = "CodeResetModelImpl";
    private CodeDeatailBean codeDeatailBean = null;


    public CodeResetModelImpl(Context context) {
        super(context);
    }

    @Override
    public void codeReset(CodeQueryInBean codeQueryInBean, final CallBack callBack) {
        try {
            RequestBody body = RequestBody.create(PDAUtil.JSON, gson.toJson(codeQueryInBean));
            httpConfig.codeReplace(body)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new io.reactivex.Observer<ResponseBody>() {
                        private Disposable mDisposable;

                        @Override
                        public void onSubscribe(Disposable d) {
                            mDisposable = d;
                        }

                        @Override
                        public void onComplete() {
                            String str="";
                            if(codeDeatailBean.getData().getCodeType().equals("箱码"))
                            {
                                if(codeDeatailBean.getData().getListSub().size()<=0){
                                    str = "数码为箱码\n"+"此码暂未建立对应关系";
                                }
                                else
                                {
                                    str = "数码为箱码\n"+"对应盒码:";
                                    for (int i = 0; i <codeDeatailBean.getData().getListSub().size() ; i++) {
                                        str= str+codeDeatailBean.getData().getListSub().get(i)+" ,";
                                    }
                                    str = str.substring(0, str.length() -1);
                                }
                            }
                            else if(codeDeatailBean.getData().getCodeType().equals("盒码")){
                                String str1 = codeDeatailBean.getData().getParentCode();
                                if(str1.equals(""))
                                {
                                    str = "数码为盒码\n"+"此码暂未建立对应关系";
                                }
                                else
                                {
                                    str = "数码为盒码\n"+"对应箱码:"+str1;
                                }
                            }
                            else
                            {
                                str = "此码暂未建立对应关系";
                            }
                            callBack.ontip(str);
                            callBack.ontipwl(codeDeatailBean.getData().getListWL());
                            mDisposable.dispose();
                        }

                        @Override
                        public void onError(Throwable e) {
                            if (codeDeatailBean.getErrmsg() != null && codeDeatailBean.getErrmsg().length() > 0) {
                                callBack.ontip(codeDeatailBean.getErrmsg());
                            } else {
                                callBack.ontip(PDAUtil.connectError);
                            }
                            callBack.ontipwl(null);
                            mDisposable.dispose();
                        }

                        @Override
                        public void onNext(ResponseBody abBean) {
                            LogUtils.w(StringProcess.responsetoString(abBean));
                            codeDeatailBean = gson.fromJson(StringProcess.responsetoString(abBean), CodeDeatailBean.class);
                            if (codeDeatailBean != null && codeDeatailBean.getErrcode() == 0) {
                                onComplete();
                            }
                            else {
                                onError(new Throwable());
                            }
                        }
                    });
        } catch (Exception e) {
            callBack.ontip(PDAUtil.connectError);
            e.printStackTrace();
        }
    }
}

